{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Days in a Month"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfDays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #一月有多少天"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>指定年份&nbsp;<code>year</code> 和月份&nbsp;<code>month</code>，返回 <em>该月天数&nbsp;</em>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>year = 1992, month = 7\n",
    "<strong>输出：</strong>31\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>year = 2000, month = 2\n",
    "<strong>输出：</strong>29\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>year = 1900, month = 2\n",
    "<strong>输出：</strong>28\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1583 &lt;= year &lt;= 2100</code></li>\n",
    "\t<li><code>1 &lt;= month &lt;= 12</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-days-in-a-month](https://leetcode.cn/problems/number-of-days-in-a-month/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-days-in-a-month](https://leetcode.cn/problems/number-of-days-in-a-month/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1992\\n7', '2000\\n2', '1900\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        if month==2:\n",
    "            if not year%400:\n",
    "                return 29\n",
    "            elif not year%100:\n",
    "                return 28\n",
    "            elif not year%4:\n",
    "                return 29\n",
    "            else:\n",
    "                return 28\n",
    "        else:\n",
    "            if month<=7:\n",
    "                return 31 if month%2 else 30\n",
    "            else:\n",
    "                return 31 if not month%2 else 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        leap = False\n",
    "        days = [-1, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if month == 2:\n",
    "            if year % 4 == 0 and (year % 100 or year % 400 == 0):   return 29\n",
    "            return 28\n",
    "        return days[month]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        mon=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        if month!=2:\n",
    "            return mon[month]\n",
    "        else:\n",
    "            if year%4==0 and year%100!=0 or year%400==0:\n",
    "                return 29\n",
    "            else:\n",
    "                return 28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        # 四年一润，百年不润，四百年再润 --》 现在该不该润？\n",
    "        month_to_day_list = [0, 31, 29, 31,30,31,30,31,31,30,31,30,31]\n",
    "        if month != 2:\n",
    "            return month_to_day_list[month]\n",
    "        else:\n",
    "            if year % 400 == 0:\n",
    "                return 29\n",
    "            elif year % 4 == 0 and year % 100 != 0:\n",
    "                return 29 \n",
    "            else:\n",
    "                return 28\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "\n",
    "        D = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:\n",
    "            D[1] += 1\n",
    "        return D[month - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        d = {1:31, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}\n",
    "\n",
    "        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:\n",
    "            d[2] = 29\n",
    "        else:\n",
    "            d[2] = 28\n",
    "\n",
    "        return d[month]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, Y: int, M: int) -> int:\n",
    "        if (Y % 100 != 0 and Y % 4 == 0) or Y % 400 == 0:\n",
    "            return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 闰年\n",
    "        else:\n",
    "            return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 非闰年"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        leap = False\n",
    "        days = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if month == 2 and year % 4 == 0 and (year % 100 or year % 400 == 0):   return 29\n",
    "        return days[month]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        def isLeapYear(y: int) -> bool:\n",
    "            return y%4==0 and y%100!=0 or y%400==0\n",
    "        a = [0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        t = a[month]\n",
    "        if month == 2 and isLeapYear(year):\n",
    "            t += 1\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "      day = dict(zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]))\n",
    "\n",
    "      def is_leap(year):\n",
    "        return (year % 100 != 0 and year % 4 == 0) or year % 400 == 0\n",
    "\n",
    "      return day[month] if month != 2 else day[month] + (1 if is_leap(year) else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        ans = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "        if year % 100 == 0:\n",
    "            if year % 400 == 0 and month == 2:\n",
    "                return 29\n",
    "            return ans[month-1]\n",
    "        else:\n",
    "            if year % 4 == 0 and month == 2:\n",
    "                return 29\n",
    "            return ans[month-1]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        months={1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}\n",
    "        res = months[month]\n",
    "        if self.is_gap(year) and month == 2:\n",
    "            res +=1\n",
    "        return res\n",
    "\n",
    "    def is_gap(self, year):\n",
    "        return year%400 == 0 or (year%4 == 0 and year%100!=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        md = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        def leap(year):\n",
    "\n",
    "            #return True\n",
    "            if year % 4 == 0:\n",
    "                if year % 100 == 0 and year % 400 == 0:\n",
    "                    return True\n",
    "                elif year % 100 == 0 and year % 400 != 0:\n",
    "                    return False\n",
    "                return True\n",
    "            return False\n",
    "\n",
    "        if leap(year) and month == 2:\n",
    "            return 29\n",
    "        elif not leap(year) and month == 2:\n",
    "            return 28\n",
    "        return md[month]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        d = {1:31, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}\n",
    "\n",
    "        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:\n",
    "            d[2] = 29\n",
    "        else:\n",
    "            d[2] = 28\n",
    "\n",
    "        return d[month]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def numberOfDays(self, Y, M):\n",
    "        \"\"\"\n",
    "        :type Y: int\n",
    "        :type M: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if (Y % 100 != 0 and Y % 4 == 0) or Y % 400 == 0:\n",
    "            return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 闰年\n",
    "        else:\n",
    "            return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 非闰年\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        if (year % 100 != 0 and year % 4 == 0) or year % 400 == 0:\n",
    "            return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1] # 闰年\n",
    "        else:\n",
    "            return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1] # 非闰年"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def numberOfDays(self, year: int, month: int) -> int:\n",
    "#         mon=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n",
    "#         if month!=2:\n",
    "#             return mon[month]\n",
    "#         else:\n",
    "#             if year%4==0 and year%100!=0 or year%400==0:\n",
    "#                 return 29\n",
    "#             else:\n",
    "#                 return 28\n",
    "\n",
    "###官解：\n",
    "class Solution(object):\n",
    "    def numberOfDays(self, Y, M):\n",
    "        \"\"\"\n",
    "        :type Y: int\n",
    "        :type M: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if (Y % 100 != 0 and Y % 4 == 0) or Y % 400 == 0:\n",
    "            return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 闰年\n",
    "        else:\n",
    "            return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M - 1] # 非闰年\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isLeapYear(self, year):\n",
    "        return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "\n",
    "\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        month_day = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        days=month_day[month-1]\n",
    "        if month==2 and self.isLeapYear(year):\n",
    "            days+=1\n",
    "        return days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfDays(self, year: int, month: int) -> int:\n",
    "        day = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        if month != 2 or not (not year % 4 and year % 100 or not year % 400): return day[month]\n",
    "        return 29"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
